import axios from "axios";
// @ts-ignore
import cookie from "js-cookie";
import {message} from "ant-design-vue";

const httpService = axios.create({
    baseURL: import.meta.env.VITE_BASE_URL,
    timeout: 60000  //60s
});

//将token放请求头
httpService.interceptors.request.use((config: any) => {
    config.headers.token = cookie.get("token")
    return config
}, error => {
    return Promise.reject(error)
})

//请求拦截器
httpService.interceptors.request.use(
    (config) => {
        config.headers = {
            Authorization: 'token'
        }
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

//响应拦截器
httpService.interceptors.response.use(
    (response) => {
        return response
    },
    (error) => {
        if (error.response.status === 401) {
            message.error("权限不足！").then(r => {
                return r
            })
        } else if (error.response.status === 403) {
            message.error("重定向错误！").then(r => {
                return r
            })
        } else if (error.response.status === 404) {
            message.error("页面不存在！").then(r => {
                return r
            })
        } else {
            message.error("异常错误，请联系管理员！").then(r => {
                return r
            })
        }
        return Promise.resolve({})
    }
)

export default httpService